package jdbc;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-18
 * Time: 17:06
 * author:SearchJdbc
 */
public class SearchJdbc {
    //根据姓名查找
    public  ResultSet searchname(String name) throws SQLException {
        //1. 创建DataSource 对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://localhost:3306/libarysystem?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");
        Connection connection = dataSource.getConnection();

        //  构造一个SQL
        String sql = "select * from booklist  where name like '%"+ name + "%'";//内容待定
        //此处光是一个String 类型的sql还不行,要把这个String 包装成一个"语句对象"
        PreparedStatement statement = connection.prepareStatement(sql);

        System.out.println(statement);

        //4.执行SQL
        ResultSet result = statement.executeQuery();
        return result;
    }

    //根据借阅人查找
    public  ResultSet searchUserid(String userid) throws SQLException {
        //1. 创建DataSource 对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://localhost:3306/libarysystem?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");
        Connection connection = dataSource.getConnection();
        //  构造一个SQL
        String sql = "select * from booklist where Borrower = ?";//内容待定
        //此处光是一个String 类型的sql还不行,要把这个String 包装成一个"语句对象"
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,userid);
        System.out.println(statement);
        //4.执行SQL
        ResultSet result = statement.executeQuery();
        return result;
    }
}
